﻿<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>左侧树型页</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link href="statics/css/left.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="statics/css/zTreeStyle.css" type="text/css">

<script type="text/javascript" src="statics/js/jquery-1.6.2.min.js"></script>
<script language="javascript" type="text/javascript" src="statics/js/ADS.js"></script>
<script type="text/javascript" src="statics/js/jquery-jtemplates.js"></script>
<script type="text/javascript" src="statics/js/jquery.ztree.core-3.1.min.js"></script>
<script type="text/javascript" src="statics/js/jquery.ztree.excheck-3.1.min.js"></script>
<script type="text/javascript">
    var basePath="<%=basePath%>";
    var bodyHeight = parent.$("#right").attr("height");
    var setting = {
    		data: {
    			simpleData: {
    				enable: true,
    				idKey: "id",
    				pIdKey: "pId",
    				rootPId: 0,
    			}
    		},
    
           check: {
		     enable: true,
		     chkStyle: "checkbox"
	       },
	       
	       callback: {
	   		 onClick: zTreeOnClick
	   	   }
    };
    
    function zTreeOnClick(event, treeId, treeNode) {
    	var treeObj = $.fn.zTree.getZTreeObj("treeOrgation");
        if(treeNode.isParent){
          var isDoExpand = treeNode.open;
          treeObj.expandNode(treeNode, !isDoExpand, false, true);
        }
        else{
           var chkStatus = treeNode.checked;
           treeObj.checkNode(treeNode, !chkStatus, true);
         }
    };
    
    function searchNodes(keywordId){
    	var keywordObj = document.getElementById(keywordId);
    	if(keywordObj != null){
    		if(keywordObj.value != ""){
    			//获得树对象
    			var treeObj = $.fn.zTree.getZTreeObj("treeOrgation");
    	    	//取消被选中的节点(和checkbox,radio中的checked属性无关)
    			var selectNodes = treeObj.getSelectedNodes();
    	    	if(selectNodes != null){
    	    		for(var idx=0; idx<selectNodes.length; idx++){
    	    			treeObj.cancelSelectedNode(selectNodes[idx]);
    	    		}
    	    	}
    	    	//获得模糊查询匹配的节点
    			var nodes = treeObj.getNodesByParamFuzzy("name", keywordObj.value, null);
    	    	for(var idx=0; idx<nodes.length; idx++){
    	    		treeObj.selectNode(nodes[idx], true);
    	    		//treeObj.expandNode(nodes[idx].getParentNode(), true, false, true);
    	    	}
    		}else{
    			alert("请输入查询条件");
    		}
    	}
    }
   
    function setCheckBox(orgIds) {
       var treeObj = $.fn.zTree.getZTreeObj("treeOrgation");
       //全部节点取消勾选
       treeObj.checkAllNodes(false);
       if(orgIds != null && orgIds != ""){
    	   var orgIdarr=orgIds.split(",");
    	   if(orgIdarr.length != 1){
    	       for(var idx=0; idx<orgIdarr.length; idx++){
    		       //var node = treeObj.getNodeByTId(orgIdarr[idx]);
    		       var node = treeObj.getNodesByParam("id", orgIdarr[idx], null);
    		       if(node != null && !node[0].isParent){
    		    	  var parentNode = node[0].getParentNode();
    		    	  if(parentNode != null){
    		    		  treeObj.expandNode(parentNode, true, false, true); 
    		    	  }
                      treeObj.checkNode(node[0], true, true);
    		       }
    	       }
    	   }else{
    		   //var node = treeObj.getNodeByTId("");
    		   var node = treeObj.getNodesByParam("id", orgIdarr, null);
    		   if(node != null && !node[0].isParent){
    			   var parentNode = node[0].getParentNode();
 		    	   if(parentNode != null){
 		    		  treeObj.expandNode(parentNode, true, false, true); 
 		    	   }
    			   treeObj.checkNode(node[0], true, true);
    		   }
    	   }　
       }
    }
    
    var zNodes = new Array(); 
    //排序查询
      $(function(){  
    	$.ajax({
    		 type:'POST',
	    	 url: "admin/treeorgation/treeorgation", 
	    	 async:false,
			 dataType: "json",
			 success:function(data){
			     $.each(data.treeMode,function(i,item){  
			    	 zNodes.push(new Node(item.id,item.pId,item.name));  
			      });  
			 }  
		   });  
    });
    
    function Node(id,pid,name){  
        this.id=id;  
        this.pId=pid;  
        this.name=name;                              
    }

    function expandTree(){  
    	var obj = $.fn.zTree.getZTreeObj("treeOrgation");
    	obj.expandAll(true);                          
    }
    function closeTree(){
    	var obj = $.fn.zTree.getZTreeObj("treeOrgation");
    	obj.expandAll(false);                              
    }
    
    function showCheckedNodes(){
    	var obj = $.fn.zTree.getZTreeObj("treeOrgation");
        var nodes = obj.getCheckedNodes(true);
        var nodeIds = "";
        for(var idx=0; idx<nodes.length; idx++){
        	if(nodes[idx].isParent != true){
        	  if(nodeIds != ""){
        		  nodeIds += "," + nodes[idx].id;
        	  }else{
        		  nodeIds =  nodes[idx].id;
        	  }
            }
        }
        return nodeIds;
    }

    $(document).ready(function() {
        document.getElementById("left_box").style.height = bodyHeight - 10 + "px";
    	var treeObj = $.fn.zTree.init($("#treeOrgation"), setting, zNodes);


    	$(document).mousedown(function(){
    		parent.isDrag = false;
    	})
    	
    		
    	$(document).mousemove(function(e){

    			parent.isDrag = false;
    			var posX = e.clientX;
    			
    			//设置左侧DIV和左侧内部iframe层的宽度
    			
    			parent.$("#drag").css("width",posX);
    			
    			e.preventDefault();

    	})
    	
    	
    	$(document).mouseup(function(){
    		if(parent.isDrag){
	    		parent.isDrag = false;
	    		
	    		parent.$("#drag").hide();
	    		parent.$("#show").hide();
	    		var width = parent.$("#drag").css("width");
	    		
	    		parent.$("#sider_box").css("width",width);
	    		parent.$("#sider_bar").css("width",parseInt(width)-10)
	    		parent.$("#sider_box").find("iframe").eq(0).attr("width",width);
	    		parent.$("#direct").css("left",parseInt(width)+25);
	    		
	    		var holderWidth = parseInt(parent.wrapperWidth)-parseInt(width)-35;
	    		
	    		parent.$("#holder").css("width",holderWidth);
	    		parent.$("#holder"),find("iframe").eq(0).attr("width",holderWidth);
	    		parent.document.frames["right"].$("#content").css("width",holderWidth);


	    		
	    		
	    		parent.$(document).unbind("mousemove",parent.dd);		
	    		//parent.document.onmousemove = null;
    		}
    	});
    		
    });
    
    </script>
</head>
<body style="overflow: auto;">
	<div class="siderbar" id="left_box" style="overflow: auto;">
		<div class="search_box">
			<input id="keywordTxt" name="keywordTxt" type="text" class="txt" />
			<input type="submit" class="btn" value="" onclick="searchNodes('keywordTxt')" />
		</div>
		<h3>组织结构</h3>
		<p>
			<a href="javascript:expandTree()">展开所有</a> | 
			<a href="javascript:closeTree();">关闭所有</a>
		</p>
		<ul id="treeOrgation" class="ztree"></ul>
		<ul id="menu">
		</ul>
	</div>
</body>
</html>
